﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArtWorldObjects;
using WorldOfArtWebApp.DataAccess;


namespace WorldOfArtWebApp.Views
{
    public partial class ArtistEntityWebForm : System.Web.UI.Page
    {
        protected ArtWorldCollectionArtist artistData;
        protected ArtWorldCollectionArtPiece workOfArtData;
        protected TitleArtist titleArtist;
        protected List<TitleArtist> titleArtistList;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadArtistView();
            LoadWorkOfArtView();
        }

        protected void LoadArtistView()
        {
            try
            {
                artistData = new ArtWorldCollectionArtist();

                if (artistData.ArtWorldCollection.Count > 0 && artistData.ExceptionMessage == string.Empty)
                {
                    this.GridViewArtist.DataSource = artistData.GetListOfArtist();
                    this.GridViewArtist.DataBind();
                }
                else
                {
                    this.labelException.Text = "The artist information you requested is not available. " + artistData.ExceptionMessage;
                }
            }
            catch (Exception ex)
            {
                this.labelException.Text = "The artist information you requested is not available. " + ex.Message;
            }
        }

        protected void LoadWorkOfArtView()
        {
            try
            {
                workOfArtData = new ArtWorldCollectionArtPiece();

                if (workOfArtData.ArtWorldCollection.Count > 0 && workOfArtData.ExceptionMessage == string.Empty)
                {
                    titleArtistList = new List<TitleArtist>();
                    foreach (var workOfArtInList in workOfArtData.GetListOfArtPiece())
                    {
                        titleArtist = new TitleArtist(workOfArtInList.TheArtist, workOfArtInList);
                        titleArtistList.Add(titleArtist);
                    }
                    this.GridViewWorkOfArt.DataSource = titleArtistList;
                    this.GridViewWorkOfArt.DataBind();
                }
                else
                {
                    this.labelException.Text = "The art information you requested is not available. " + workOfArtData.ExceptionMessage;
                }
            }
            catch (Exception ex)
            {
                this.labelException.Text = "The art information you requested is not available. " + ex.Message;
            }
        }
    }


    /// <summary>
    /// Helper class for binding data to the Work of art gridview 
    /// </summary>
    public class TitleArtist
    {
        protected string m_artistName;
        protected string m_title;

        public TitleArtist()
        {
        }

        public TitleArtist(Artist artist, ArtPiece workOfArt)
        {
            m_artistName = artist.Name;
            m_title = workOfArt.Title;
        }

        public string ArtistName
        {
            get { return m_artistName; }
        }

        public string Title
        {
            get { return m_title; }
        }
    }
}